package com.hskn.hss.module.tldicttype.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tldicttype.entity.TlEmergentDictType;
import com.hskn.hss.module.tldicttype.mapper.TlEmergentDictTypeMapper;
import com.hskn.hss.module.tldicttype.service.ITlEmergentDictTypeService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-18
 */
@Service
public class TlEmergentDictTypeServiceImpl extends ServiceImpl<TlEmergentDictTypeMapper, TlEmergentDictType> implements ITlEmergentDictTypeService {

    @Resource
    TlEmergentDictTypeMapper tlEmergentDictTypeMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlEmergentDictType tlEmergentDictType = params.getObject("data", TlEmergentDictType.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        SysUserDepart sysUserDepart = sysUserDepartMapper.selectOne(queryWrapper);
        if (null != sysUserDepart) {
            tlEmergentDictType.setDeptid(sysUserDepart.getDepid());
        }
        tlEmergentDictType.setCreateby(uid);
        tlEmergentDictType.setCreatetime(new Date());
        if (tlEmergentDictType.getStatus() == null || tlEmergentDictType.getStatus().isEmpty()) {
            tlEmergentDictType.setStatus("1");
        }
        tlEmergentDictTypeMapper.insert(tlEmergentDictType);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlEmergentDictType tlEmergentDictType = params.getObject("data", TlEmergentDictType.class);
        tlEmergentDictType.setUpdateby(uid);
        tlEmergentDictType.setUpdatetime(new Date());
        tlEmergentDictTypeMapper.updateById(tlEmergentDictType);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlEmergentDictTypeMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlEmergentDictType getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlEmergentDictType tlEmergentDictType = tlEmergentDictTypeMapper.selectById(id);
        return tlEmergentDictType;
    }

    @Override
    public List getSelectList() {
        List resultList = tlEmergentDictTypeMapper.getSelectList();
        return resultList;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("eventtype", params.getString("eventtype"));
        map.put("status", params.getString("status"));
        Page<TlEmergentDictType> listPage = new Page<>(page, size, true);
        IPage<TlEmergentDictType> iPage = tlEmergentDictTypeMapper.listTlEmergentDictType(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("resultlist", iPage.getRecords());
        return json;
    }
}
